package com.biyao.service.model.base.imp;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.HashMap;
import java.util.Map;

import com.biyao.schedulingdao.base.TBearingBoxMapper;
import com.biyao.schedulingentity.base.TBearingBoxEntity;
import com.biyao.schedulingentity.base.TBearingBoxTypeEntity;
import com.biyao.schedulingentity.base.TRollerBasicEntity;
import com.biyao.service.model.base.TBearingBoxMessage;
import com.biyao.service.model.base.TBearingBoxTypeMessage;
import com.biyao.service.model.base.TRollerBasicMessage;
import com.biyao.service.model.state.RollerExtMessage;
import com.biyao.service.model.state.imp.TRollerExtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;
/**
 * 
 *
 * @author code generator
 * @date 2019-09-16 09:44:07
 */
@Service
public class TBearingBoxService extends ServiceImpl<TBearingBoxMapper, TBearingBoxEntity> implements TBearingBoxMessage {

    @Autowired
    private TBearingBoxTypeMessage tBearingBoxTypeMessage;
    @Autowired
    private TRollerBasicMessage tRollerBasicMessage;

    @Autowired
    private RollerExtMessage rollerExtMessage;
    public Page list(int pageNum, int pageSize){

        EntityWrapper<TBearingBoxEntity> tBearingBoxentityWrapper=new EntityWrapper<>();
        Page page=selectPage(new Page(pageNum,pageSize),tBearingBoxentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TBearingBoxEntity> list){
        List resList=new ArrayList();
        for(TBearingBoxEntity tBearingBoxEntity:list){
            Map map=formatEntity(tBearingBoxEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TBearingBoxEntity tBearingBoxEntity){
        Map map=new HashMap(16);
        //轴承箱信息
        if(tBearingBoxEntity.getId()!=null){
           map.put("id",tBearingBoxEntity.getId());
        }
        //编号
        if(tBearingBoxEntity.getCode()!=null){
           map.put("code",tBearingBoxEntity.getCode());
        }
        //型号
        if(tBearingBoxEntity.getModel()!=null){
           map.put("model",tBearingBoxEntity.getModel());
        }
        //生产厂家
        if(tBearingBoxEntity.getManufacturer()!=null){
           map.put("manufacturer",tBearingBoxEntity.getManufacturer());
        }
        //生产日期
        if(tBearingBoxEntity.getManufactureDate()!=null){
           map.put("manufactureDate",tBearingBoxEntity.getManufactureDate());
        }
        //投产日期
        if(tBearingBoxEntity.getProductionDate()!=null){
           map.put("productionDate",tBearingBoxEntity.getProductionDate());
        }
        //
        if(tBearingBoxEntity.getBearingBoxTypeId()!=null){
           map.put("bearingBoxTypeId",tBearingBoxEntity.getBearingBoxTypeId());
           TBearingBoxTypeEntity tBearingBoxTypeEntity=tBearingBoxTypeMessage.selectBearingBox(tBearingBoxEntity.getBearingBoxTypeId());
           if(tBearingBoxTypeEntity!=null){
               map.put("boxBoxType",tBearingBoxEntity.getCode());
           }
        }
        //目前状态
        if(tBearingBoxEntity.getState()!=null){
           map.put("state",tBearingBoxEntity.getState());
        }
        //轧辊编号
        if(tBearingBoxEntity.getRollerBaseCode()!=null){
           map.put("rollerBaseCode",tBearingBoxEntity.getRollerBaseCode());
        }
        //保养日期
        if(tBearingBoxEntity.getMaintenanceDate()!=null){
           map.put("maintenanceDate",tBearingBoxEntity.getMaintenanceDate());
        }
        //
        if(tBearingBoxEntity.getRemark()!=null){
           map.put("remark",tBearingBoxEntity.getRemark());
        }
        //
        if(tBearingBoxEntity.getCreateTime()!=null){
           map.put("createTime",tBearingBoxEntity.getCreateTime());
        }
        //
        if(tBearingBoxEntity.getUpdateTime()!=null){
           map.put("updateTime",tBearingBoxEntity.getUpdateTime());
        }
        //
        if(tBearingBoxEntity.getCreateBy()!=null){
           map.put("createBy",tBearingBoxEntity.getCreateBy());
        }
        //
        if(tBearingBoxEntity.getUpdateBy()!=null){
           map.put("updateBy",tBearingBoxEntity.getUpdateBy());
        }
        if(tBearingBoxEntity.getRollerNewId()!=null){
            TRollerBasicEntity tRollerBasicEntity= tRollerBasicMessage.selectRollerBasicById(tBearingBoxEntity.getRollerNewId()) ;
            if(tRollerBasicEntity!=null){
                map.put("rollerNewCode",tRollerBasicEntity.getCode());
            }
        }
        if(tBearingBoxEntity.getRollerNewId()!=null){
            map.put("rollerNewId",tBearingBoxEntity.getRollerNewId());
        }
        return map;
    }

    @Override
    public TBearingBoxEntity selectBearingBoxByCode(String code) {
        EntityWrapper<TBearingBoxEntity> tBearingBoxentityWrapper=new EntityWrapper<>();
        tBearingBoxentityWrapper.eq("roller_base_code",code);
        List<TBearingBoxEntity> list=selectList(tBearingBoxentityWrapper);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public TBearingBoxEntity selectBearingBoxById(Integer id) {
        return selectById(id);
    }

    @Override
    public List<Map> selectBearingBoxList() {
        EntityWrapper<TBearingBoxEntity> tBearingBoxentityWrapper=new EntityWrapper<>();
        return formatRecords(selectList(tBearingBoxentityWrapper));
    }

    public Boolean selectCodeIsRepeat(String code) {
        EntityWrapper<TBearingBoxEntity> bearingBoxEntityEntityWrapper=new EntityWrapper<>();
        bearingBoxEntityEntityWrapper.eq("code",code);
        List<TBearingBoxEntity> list=selectList(bearingBoxEntityEntityWrapper);
        if(list.size()>0){
            return true;
        }
        return false;
    }

    public Boolean selectUpdateCodeIsRepeat(String code,Integer id) {
        EntityWrapper<TBearingBoxEntity> bearingBoxEntityEntityWrapper=new EntityWrapper<>();
        bearingBoxEntityEntityWrapper.eq("code",code);
        List<TBearingBoxEntity> list=selectList(bearingBoxEntityEntityWrapper);
        if(list.size()>0){
            if(list.get(0).getId().intValue()!=id.intValue()){
                return true;
            }
            return false;
        }
        return false;
    }

    public Map getRollerByBox(Integer id){
        TBearingBoxEntity tBearingBoxEntity= selectById(id);
        if(tBearingBoxEntity!=null){
            if(tBearingBoxEntity.getRollerNewId()!=null){
              Map map=rollerExtMessage.selectRollerExtByRollerId(tBearingBoxEntity.getRollerNewId());
              return map;
            }
        }
        return null;
    }
}

